package Entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class Benefit implements Serializable {
	
	private String logo;
	private String name;
	private String description;
	private String fullAddress;
	private String company;
	private String distance;
	private String map;	
	private String category;
	private double latitude;	
	private double longitude;
	private long endDateTime;	
	private int id;
	
	public double getLatitude() {
		return latitude;
	}

	public void setLatitude(double latitude) {
		this.latitude = latitude;
	}

	public double getLongitude() {
		return longitude;
	}

	public void setLongitude(double longitude) {
		this.longitude = longitude;
	}
	
	public String getMap() {
		return map;
	}

	public void setMap(String map) {
		this.map = map;
	}

	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public long getEndDateTime() {
		return endDateTime;
	}

	public void setEndDateTime(long endDateTime) {
		this.endDateTime = endDateTime;
	}

	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getFullAddress() {
		return fullAddress;
	}

	public void setFullAddress(String fullAddress) {
		this.fullAddress = fullAddress;
	}

	public String getCompany() {
		return company;
	}

	public void setCompany(String company) {
		this.company = company;
	}

	public String getDistance() {
		return distance;
	}

	public void setDistance(String distance) {
		this.distance = distance;
	}


	
	public Benefit(int id) {

		this.name = "Benefit " + id;
		this.setId(id);
		this.setLogo("http://cdn4.droidmill.com/media/market-media/com.health.rss_icon.png");
		
	}
	
	public Benefit(JSONObject obj) {
		try {
			this.name = obj.getString("name");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			this.logo = obj.getString("icon");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			this.latitude = obj.getDouble("latitude");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			this.longitude = obj.getDouble("longitude");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		
		try {
			this.setId(obj.getInt("id"));
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			this.setDescription(obj.getString("description"));
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			this.setFullAddress(obj.getString("fullAdress"));
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			this.setCompany(obj.getString("company"));
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			this.setDistance(obj.getString("distance"));
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			this.setMap(obj.getString("map"));
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			this.setCategory(obj.getString("category"));
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			this.setEndDateTime(obj.getLong("endDateTime"));
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	
	public static ArrayList<Benefit> createMocks(){
		ArrayList<Benefit> items = new ArrayList<Benefit>();
		for (int i = 0; i < 8; i++) {
			Benefit p  = new Benefit(i);
			items.add(p);
		}
		return items;
	}
	
	public static ArrayList<Benefit> getFromJSON(JSONObject itemsJSON){
		ArrayList<Benefit> items = new ArrayList<Benefit>();

		JSONArray itemsArray;
		try {
			itemsArray = itemsJSON.getJSONArray("benefits");
			for(int i=0;i<itemsArray.length();i++){			
				Benefit p = new Benefit(itemsArray.getJSONObject(i));				
				items.add(p);					
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return items;
	}


	public void setLogo(String logo) {
		this.logo = logo;
	}

	public String getLogo() {
		return logo;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getId() {
		return id;
	}
}
